/**
 * @description 配置redis服务器
 * @author xu148 fengye.cn @秋枫
 * @create 2025/6/25
 */

package cn.fengye.config;


import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@EnableConfigurationProperties(RedisAutoProperties.class)
public class RedisClientConfig {
    @Bean("redissonClient")
    public RedissonClient redissonClient(RedisAutoProperties redisAutoProperties) {
        Config config = new Config();
        config.setCodec(JsonJacksonCodec.INSTANCE);

        config.useSingleServer()
                .setAddress("redis://" + redisAutoProperties.getHost() + ":" + redisAutoProperties.getPort())
                .setPassword(redisAutoProperties.getPassword())
                .setConnectionPoolSize(redisAutoProperties.getPoolSize())
                .setConnectionMinimumIdleSize(redisAutoProperties.getMinIdleSize())
                .setIdleConnectionTimeout(redisAutoProperties.getIdleTimeout())
                .setConnectTimeout(redisAutoProperties.getConnectTimeout())
                .setRetryAttempts(redisAutoProperties.getRetryAttempts())
                .setRetryInterval(redisAutoProperties.getRetryInterval())
                .setPingConnectionInterval(redisAutoProperties.getPingInterval())
                .setKeepAlive(redisAutoProperties.isKeepAlive())
        ;

        RedissonClient redissonClient = Redisson.create(config);

        log.info("redis服务器初始化构造完成。{} {} {}", redisAutoProperties.getHost(), redisAutoProperties.getPoolSize(), !redissonClient.isShutdown());

        return redissonClient;
    }
}
